<template>
  <div class="login-container">
    <vue-particles
      color="#dedede"
      :particleOpacity="0.7"
      :particlesNumber="80"
      shapeType="circle"
      :particleSize="4"
      linesColor="#dedede"
      :linesWidth="1"
      :lineLinked="true"
      :lineOpacity="0.4"
      :linesDistance="150"
      :moveSpeed="3"
      :hoverEffect="true"
      hoverMode="grab"
      :clickEffect="true"
      clickMode="push"
    ></vue-particles>
    <div class="login-box">
      <!--头像-->
      <div class="avatar-box">
        <img src="../../assets/logo.png" alt />
      </div>
      <!--表单-->
      <el-form
        label-width="0px"
        class="login-form"
        :model="loginForm"
        :rules="loginRules"
        ref="loginFormRef"
      >
        <el-form-item prop="username">
          <el-input prefix-icon="fas fa-user" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="fas fa-lock" v-model="loginForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
        </el-form-item>
        <div class="btns-text">
          <el-button type="text" @click="resetForm">重置表单</el-button>
          <el-button type="text" style="">忘记密码 ?</el-button>
        </div>
          
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    var validateUsername = (rule, value, callback) => {
      var patt = /^[_,A-z,0-9]{3,10}$/i
      if (!patt.test(value)) {
        callback(new Error('用户名必须是字母下划线或数字'))
      }
      if(value !== 'airvip') {
        callback(new Error('用户名必须是airvip'))
      }
      callback()
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3-10 个字符', trigger: 'blur' },
          { validator: validateUsername, trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    resetForm() {
      this.$refs.loginFormRef.resetFields()
      // console.log(this)
    },
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        await this.$http
          .post('login', this.loginForm)
          .then(res => {
            // console.log(res);
            res = res.data
            if (!res.code || res.code == 0)
              return this.$message.error(res.msg || '登录失败')
            this.$message.success('登录成功')
            window.sessionStorage.setItem(
              'token',
              res.data.token || 'airvip is b boy!'
            )
            this.$router.push('/home')
          })
          .catch(err => {
            return this.$message.error(err.message)
          })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
$bg: #2d3a4b;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-box {
    width: 450px;
    height: 350px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .avatar-box {
      width: 130px;
      height: 130px;
      border: 1px solid #eeeeee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #dddddd;
      position: relative;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #eeeeee;

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: $bg;
      }
    }

    .login-form {
      position: absolute;
      bottom: 0px;
      width: 100%;
      padding: 20px;
      box-sizing: border-box;

      .btns {
        display: block;
        .el-button {
          width: 100%;
        }
      }

      .btns-text {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>